package com.binvshe.binvshe.message.dao;


import com.binvshe.binvshe.app.MyApplication;

import java.util.List;

/**
 * Created by Zoi.
 * E-mail：KyluZoi@gmail.com
 * 2016/7/22
 */
public class MessageHelper {
    private static MessageHelper ourInstance = new MessageHelper();
    private static MessageDao msgDao;

    public static synchronized MessageHelper getInstance() {
        if (msgDao == null) {
            synchronized (MessageHelper.class) {
                if (msgDao == null) {
                    msgDao = MyApplication.getDaoSession(MyApplication.getAppContext()).getMessageDao();
                }
            }
        }
        return ourInstance;
    }

    private MessageHelper() {
    }


    /**
     * Method Name: loadUnreadNumType
     */
    public static int loadUnreadNumType(int messageType) {
        int count = msgDao.queryBuilder()
                .where(MessageDao.Properties.IsRead.eq(false))
                .where(MessageDao.Properties.Type.eq(messageType))
                .list().size();
        return count;
    }

    /**
     * Method Name: updataIsRead
     */
    public static void updataIsRead(Message message) {
        msgDao.update(message);
        return;
    }


    /**
     * 更新已读
     */
    public void updataIsRead(Message ...messages) {
        if (messages != null && messages.length > 0) {
            for (Message msg : messages) {
                msg.setIsRead(true);
            }
            msgDao.updateInTx(messages);
        }
    }

    /**
     * Method Name: insertNewMessage
     */
    public static void insertNewMessage(Message message) {
        msgDao.insert(message);
    }

    /**
     * Method Name: deleteMessage
     */
    public static void deleteMessage(Message message) {
        msgDao.delete(message);
    }

    /**
     * Method Name: loadMessage
     */
    public static List<Message> loadMessage(int messageType) {
        List<Message> list = msgDao.queryBuilder()
                .where(MessageDao.Properties.Type.eq(messageType))
                .orderDesc(MessageDao.Properties.Id)
                .list();
        return list;
    }

    public static List<Message> loadMessage(int messageType1, int messageType2) {
        List<Message> list = msgDao.queryBuilder()
                .whereOr(MessageDao.Properties.Type.eq(messageType1),
                        MessageDao.Properties.Type.eq(messageType2))
                .orderDesc(MessageDao.Properties.Id)
                .list();
        return list;
    }

    public static List<Message> loadMessage(int messageType1, int messageType2, int messageType3, int messageType4) {
        List<Message> list = msgDao.queryBuilder()
                .whereOr(MessageDao.Properties.Type.eq(messageType1),
                        MessageDao.Properties.Type.eq(messageType2),
                        MessageDao.Properties.Type.eq(messageType3),
                        MessageDao.Properties.Type.eq(messageType4))
                .orderDesc(MessageDao.Properties.Id)
                .list();
        return list;
    }

    /**
     * Method Name: loadMessageAll
     */
    public static List<Message> loadMessageAll() {
        List<Message> list = msgDao.loadAll();
        return list;
    }

    public static void deleteMessageAll(){
        msgDao.deleteAll();
    }

    public MessageDao getMsgDao() {
        return msgDao;
    }

}
